<?php
use yii\helpers\Url;
use yii\web\View;
use app\assets\SolutionBasicAsset;
SolutionBasicAsset::register($this);
$this->title = $this->context->pageTitle.Yii::$app->params['titleSuffix'];
$this->params['breadcrumbs'] = [
  [
    'label' => \Yii::t('solution', 'Solution'),
    'url' => ['solution-list']
  ],
   \Yii::t('solution', 'Edit PC Solution')
];
$jsMsgs = $this->context->loadJsMsgs([
  'dateRange',
  'solutionForm'
]);
$this->registerJs("VAM.setVar('LANGUAGE_JS', '".$jsMsgs."');", View::POS_HEAD);
?>
<div class="page-header">
  <h1>
    <?=\Yii::t('solution', 'Solution')?>
    <small><?=\Yii::t('solution', 'Edit PC Solution')?></small>
  </h1>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <i class="fa fa-edit"></i>
    <?=\Yii::t('solution', 'Edit PC Solution')?>
    <ul class="list-unstyled pull-right">
      <li>
        <a href="#">
          <i class="fa fa-chevron-up"></i>
        </a>
      </li>
    </ul>
  </div>
  <div class="panel-body">
    <div class="panel-heading">
      <h3 id="strategy_name"><?=$solution['name']?></h3>
    </div>
    <div class="step-container">
      <ul class="step-wrap">
        <li class="step-undone step-done">
          <a href="javascript:void(0);" title="<?=\Yii::t('common', 'Add Label')?>">
            <i class="fa fa-check"></i>
          </a>
          <p><?=\Yii::t('common', 'Add Label')?></p>
        </li>
        <li class="step-undone step-done">
          <a href="javascript:void(0);" title="<?=\Yii::t('common', 'Add Campaign')?>">
            <i class="fa fa-check"></i>
          </a>
          <p><?=\Yii::t('common', 'Add Campaign')?></p>
        </li>
         <li class="step-undone step-active">
          <a href="javascript:void(0);" title="<?=\Yii::t('common', 'Add Solution')?>">
            <i class="fa fa-chevron-right"></i>
          </a>
          <p><?=\Yii::t('common', 'Add Solution')?></p>
        </li>
         <li class="step-undone">
          <a href="javascript:void(0);" title="<?=\Yii::t('common', 'Add Creative')?>">
            <i class="fa fa-chevron-right"></i>
          </a>
          <p><?=\Yii::t('common', 'Add Creative')?></p>
        </li>
      </ul>
      <div class="step-progress-bar">
        <div class="step-progress" style="width: 66%;"></div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <div class="section-step">
          <ol class="list-inline main-flow">
            <li class="first current">
              <a href="<?= Url::to(['solution/pc-solution-basic-edit']);?>?solution_id=<?= $solution['id'] ?>" class="cancleFocus">
                <b>①</b>
                <?=\Yii::t('solution', 'Basic')?>
                <span><i class="fa fa-arrow-right"></i></span>
              </a>
            </li>
            <li>
              <a href="<?= Url::to(['pc-solution-target-edit']);?>?solution_id=<?= $solution['id'] ?>" class="cancleFocus">
                <b>②</b>
                <?=\Yii::t('solution', 'Condition')?>
                <span><i class="fa fa-arrow-right"></i></span>
              </a>
            </li>
            <li>
              <a href="<?= Url::to(['pc-solution-resource-edit']);?>?solution_id=<?= $solution['id'] ?>" class="cancleFocus">
                <b>③</b>
                <?=\Yii::t('solution', 'Resource')?>
                <span><i class="fa fa-arrow-right"></i></span>
              </a>
            </li>
            <li class="last">
              <a href="<?=Url::to(['pc-solution-creative-edit']);?>?solution_id=<?= $solution['id'] ?>" class="cancleFocus">
                <b>④</b>
                <?=\Yii::t('solution', 'Creative')?>
              </a>
            </li>
          </ol>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <form action="<?php echo Url::to(['solution/new-solution-handle']);?>" method="post" class="form-horizontal" id="data-frm">
          <input class="hidden" type="text" name="id" value="<?=$solution['id']?>">
          <input class="hidden" type="text" name="solution_audit" value="<?=$solution['solution_audit']?>">
          <input class="hidden" type="text" name="old_budget" value="<?=$solution['budget']?>">
          <div class="form-group">
            <label for="strategy_name" class="col-sm-2 control-label">
              <em>*</em>
              <?=\Yii::t('solution', 'Solution Name')?>：
            </label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="strategy_name" name="name" value="<?=$solution['name']?>" placeholder="<?=\Yii::t('solution', 'Solution Name')?>">
              <div class="help-block text-error" id="strategy_name_err"></div>
            </div>
          </div>
          <fieldset>
            <legend><?=\Yii::t('solution', 'Basic')?></legend>
            <div class="form-group">
              <label for="belong_launch" class="col-sm-2 control-label">
                <em>*</em>
                <?=\Yii::t('solution', 'Campaign')?>：
              </label>
              <div class="col-sm-10">
                <select name="campaign_id" class="form-control" id="belong_launch">
                <?php 
                  foreach ($campaigns as $campaign) {
                    $id = $campaign['id'];
                    $name = $campaign['name'];
                    $selected = '';
                    if (isset($bidCampaignId) && $id == $bidCampaignId) $selected = 'selected';
                    echo "<option value='$id' $selected>$name</option>";
                  }
                ?>
                </select>
              </div>
            </div>
          <div class="form-group">
            <label for="budget_num" class="col-sm-2 control-label">
              <em>*</em>
              <?=\Yii::t('solution', 'Solution Budget')?>：
            </label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="budget_num" name="budget" value="<?=$solution['budget']?>" placeholder="<?=\Yii::t('solution', 'Solution Budget')?>">
              <div class="help-block"><?php if(isset($bidCampaignId)) echo \Yii::t('solution', 'Solution budget range').'：<span class="min-budget-num">'.$budgetRange[1].'</span> ~ <span class="max-budget-num">'.$budgetRange[0]; ?></span></div>
              <div class="help-block text-error" id="budget_num_err"></div>
            </div>
          </div>
            <div class="form-group">
              <label for="range_date" class="col-sm-2 control-label">
                <em>*</em>
                <?=\Yii::t('common', 'Date Range')?>：
              </label>
              <div class="col-sm-10">
                <input type="hidden" id="start-range-date" value="<?php if(isset($bidCampaignId)) echo $bidCampaign['start_date']; ?>">
                <input type="hidden" id="end-range-date" value="<?php if(isset($bidCampaignId)) echo $bidCampaign['end_date']; ?>">
                <input type="hidden" id="start-date" value="<?php if(isset($bidCampaignId)) echo $solution['start_date']; ?>">
                <input type="hidden" id="end-date" value="<?php if(isset($bidCampaignId)) echo $solution['end_date']; ?>">
                <div class="input-group">
                  <span class="input-group-btn">
                    <button class="btn btn-default" id="range-date-btn" type="button"><i class="fa fa-calendar"></i></button>
                  </span>
                  <input type="text" class="form-control" id="range_date" name="range_date" value="<?=$solution['start_date'].' - '.$solution['end_date'] ?>" placeholder="<?=\Yii::t('common', 'Select Date Range')?>，<?=\Yii::t('common', 'format YYYY-MM-DD - YYYY-MM-DD')?>" readonly>
                </div>
                <div class="help-block"><?php if(isset($bidCampaignId)) echo \Yii::t('solution', 'Campaign date range').'：<span id="date-range-text">'.$bidCampaign['start_date'].' - '. $bidCampaign['end_date']; ?><?php if ($solutionDateRange) { list($beg, $end) = $solutionDateRange;
                 echo ", ".\Yii::t('solution', 'must include').date('Y-m-d', strtotime($beg)) .' - '.date('Y-m-d', strtotime($end));
                } ?></span></div>
                <div class="help-block text-error" id="range_date_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label for="bidding_type" class="col-sm-2 control-label">
                <em>*</em>
                <?=\Yii::t('solution', 'Bidding Type')?>：
              </label>
              <div class="col-sm-10">
                <div class="row">
                  <div class="col-sm-12 form-group">
                  <label class="radio-inline col-sm-3">
                    <input type="radio" class="bid-setting" name="is_pmp" value='0' 
					<?php
						if ($solution['is_pmp'] == 0) echo "checked";
					?> 
					>RTB
                  </label>
                  <label class="radio-inline col-sm-3">
                    <input type="radio" class="bid-setting" name="is_pmp" value='1'
					<?php
						if ($solution['is_pmp'] == 1) echo "checked";
					?> 
					>PMP
                  </label> 
                  </div>
                </div>
                <div class="" id="rtb_bid">
                  <div class="row">
                    <div class="col-sm-4">
                      <label class="radio-inline">
                        <input type="radio" class="bid-type" name="bid_type" value='1' <?php if ($solution['bid_type'] == 1) echo "checked"; ?> > <?=\Yii::t('solution', 'Fixed Bidding')?>(CPM/￥)
                      </label>
                    </div>
                    <div class="col-sm-6">
                      <input type="text" class="form-control" id="constant_bidding_num" data-err="constant_bidding_num_err" name="fix_bid_price"  placeholder="" <?php if ($solution['bid_type'] == 1) echo 'value="'.$solution['fix_bid_price'].'"'; ?>>
                      <div class="help-block text-error" id="constant_bidding_num_err"></div>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-sm-12">
                      <label class="radio-inline">
                        <input type="radio" class="bid-type" name="bid_type" value='2' <?php if ($solution['bid_type'] == 2) echo "checked"; ?>> <?=\Yii::t('solution', 'CPC Optimize Bidding')?>
                      </label>
                    </div>
                    <div class="col-sm-6">
                      <div class="form-group">
                        <label for="pcp_target_bidding_num" class="col-sm-6 control-label plr0"><?=\Yii::t('solution', 'Target Bidding')?>(CPC/￥)：</label>
                        <div class="col-sm-6 plr0">
                          <input type="text" class="form-control" id="pcp_target_bidding_num" data-err="pcp_target_bidding_num_err" name="optimize_bid_cpc" placeholder="" <?php if ($solution['bid_type'] == 2) echo 'value="'.$solution['optimize_bid_cpc'].'"'; ?>>
                          <div class="help-block text-error" id="pcp_target_bidding_num_err"></div>
                        </div>
                      </div>
                    </div>
                    <div class="col-sm-6">
                      <div class="form-group">
                        <label for="pcp_max_bidding_num" class="col-sm-6 control-label plr0"><?=\Yii::t('solution', 'Max Bidding')?>(CPM/￥)：</label>
                        <div class="col-sm-6 plr0">
                          <input type="text" class="form-control" id="pcp_max_bidding_num" data-err="pcp_max_bidding_num_err" name="optimize_bid_cpm" placeholder="" <?php if ($solution['bid_type'] == 2) echo 'value="'.$solution['optimize_bid_cpm'].'"'; ?>>
                          <div class="help-block text-error" id="pcp_max_bidding_num_err"></div>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="row hidden">
                    <div class="col-sm-12">
                      <label class="radio-inline">
                        <input type="radio" name="bid_type" value='3' <?php if ($solution['bid_type'] == 3) echo "checked"; ?>> <?=\Yii::t('solution', 'CTR Optimize Bidding')?>
                      </label>
                    </div>
                    <div class="col-sm-6">
                      <div class="form-group">
                        <label for="ctr_target_bidding_num" class="col-sm-6 control-label plr0"><?=\Yii::t('solution','Target CTR')?> (‰)：</label>
                        <div class="col-sm-6 plr0">
                          <input type="text" class="form-control" id="ctr_target_bidding_num" data-err="ctr_target_bidding_num_err" name="optimize_ctr_target" placeholder="" <?php if ($solution['bid_type'] == 3) echo 'value="'.$solution['optimize_ctr_target'].'"'; ?>>
                          <div class="help-block text-error" id="ctr_target_bidding_num_err"></div>
                        </div>
                      </div>
                    </div>
                    <div class="col-sm-6">
                      <div class="form-group">
                        <label for="ctr_max_bidding_num" class="col-sm-6 control-label plr0"><?=\Yii::t('solution', 'Max Bidding')?>(CPM/￥)：</label>
                        <div class="col-sm-6 plr0">
                          <input type="text" class="form-control" id="ctr_max_bidding_num" data-err="ctr_max_bidding_num_err" name="optimize_ctr_cpm" placeholder="" <?php if ($solution['bid_type'] == 3) echo 'value="'.$solution['optimize_ctr_cpm'].'"'; ?>>
                          <div class="help-block text-error" id="ctr_max_bidding_num_err"></div>
                        </div>
                      </div>
                    </div>
                  </div> 
                </div>
                <div class="" id="pmp_bid">
                  <div class="form-group">
                    <label for="pmp_id" class="control-label col-sm-3 text-right">
                      PMP ID:
                    </label>
                    <!-- <div class="col-sm-5 pl0">
                      <input type="text" id="pmp_id" data-err="pmp_id_err" class="form-control">
                      <div class="help-block text-error" id="pmp_id_err"></div>
                    </div> -->
                    <div class="col-sm-5 pl0">
                      <select name="pmp_guid" id="pmp_id" class="form-control col-sm-5 pl0" data-err="pmp_id_err" >
                        <option value=""></option>
                        <?php
							$selectedGuid = '';
							if ($solution['is_pmp'] == 1) {
								$selectedGuid = $solution['pmp_guid'];
							}
							foreach ($pmps as $pmp) {								
								$selected = '';
								if ($selectedGuid) {
									if ($selectedGuid == $pmp['guid']) {
										$selected = 'selected';
									}
								}
								echo $pmpOption = <<<FINE
									<option value="{$pmp['guid']}" $selected>{$pmp['guid']} - {$pmp['name']}</option>
FINE;
							}
						?>
                      </select>
                      <div class="help-block text-error" id="pmp_id_err"></div>
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="fixed_bid" class="control-label col-sm-3 text-right">
                      <?=\Yii::t('solution', 'Fixed Bidding')?>(CPM/￥):
                    </label>
                    <div class="col-sm-5 pl0">
                      <input type="text" id="fixed_bid" class="form-control" name="pmp_price" value=
					  "<?php 
						if ($solution['is_pmp']) echo $solution['pmp_price'];
					  ?>" readonly>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label for="belong_campaign" class="col-sm-2 control-label">
                <em>*</em>
                <?=\Yii::t('solution', 'Solution Mode')?>：
              </label>
              <div class="col-sm-10">
                <div class="form-group col-sm-5">
                  <label for="solution_mode" class="radio-inline">
                    <input type="radio" name="solution_mode" value="1" <?php if ($solution['solution_mode'] == 1) echo 'checked'; ?>><?=\Yii::t('solution', 'Average')?>
                  </label>
                  <div class="help-block text-error"><?=\Yii::t('solution', 'Budget per hour of the budget')?></div>               
                </div>
                <div class="form-group col-sm-5">
                  <label for="solution_mode" class="radio-inline">
                    <input type="radio" name="solution_mode" value="2" <?php if ($solution['solution_mode'] == 2) echo 'checked'; ?>><?=\Yii::t('solution', 'Accelerate')?>
                  </label>
                  <div class="help-block text-error"><?=\Yii::t('solution', 'Spend as quickly as possible in an hour')?></div>                 
                </div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <div class="form-group">
              <div class="col-sm-offset-2 col-sm-10">
                <div class="btn-form-group pull-right">
                  <input class="operate-type" type="hidden" name="operate_type" value="save">
                  <button type="submit" class="btn btn-primary save-btn" data-operate="save" data-loading-text="<i class='fa fa-spinner fa-spin'></i> <?=\Yii::t('common', 'Saving')?>..."><?=\Yii::t('common', 'Save')?></button>
                  <button type="submit" class="btn btn-success save-next-btn" data-operate="save-next" data-loading-text="<i class='fa fa-spinner fa-spin'></i> <?=\Yii::t('common', 'Saving')?>...">
                    <?=\Yii::t('common', 'Save and Next')?>
                    <i class="fa fa-caret-right"></i>
                  </button>
                  <a href="<?php echo Url::to(['solution/solution-list']); ?>" class="btn btn-default">
                    <?=\Yii::t('common', 'Back')?>
                  </a>
                </div>
              </div>
            </div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<?php
  $nextUrl = Url::to(['solution/pc-solution-target-edit']);
  $switchCampaignUrl = Url::to(['solution/get-campaign-attributes']);
  //$pmpFixedBid = Yii::getAlias('@web') . '/response/solution/fixedBid.php';
  $pmpFixedBid = Url::to(['solution/get-pmp-price']);
  $this->registerJs("VAM.setVar('SWTICH_CAMPAIGN_URL','$switchCampaignUrl');",View::POS_END);
  $this->registerJs("VAM.setVar('NEXT_URL','$nextUrl');",View::POS_END);
  $this->registerJs("VAM.setVar('PMP_FIXED_BID','$pmpFixedBid');",View::POS_END);
?>
<script type="text/javascript" >
  requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/site.js"], function(common) {
    requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/solution/newPcSolutionBasic.js"]);
  });
</script>
